package com.bdqn.t330.ch12file.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t330.ch12file.config.UploadAliYunUtil;
import com.bdqn.t330.ch12file.entity.RightEntity;
import com.bdqn.t330.ch12file.entity.Role;
import com.bdqn.t330.ch12file.entity.User;
import com.bdqn.t330.ch12file.servcie.RightService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/right")
public class RightController {

    @Resource
    RightService rightService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @RequestMapping("/list")
    public Object list(Model model,
                       @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "rightCode", required = false, defaultValue = "") String rightCode,
                       @RequestParam(value = "rightText", required = false, defaultValue = "") String rightText) {
        Page page = new Page(pageIndex, 5);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (rightCode.trim() != null && !rightCode.trim().equals("")) {
            queryWrapper.eq("right_code", rightCode.trim());
        }
        if (rightText.trim() != null && !rightText.trim().equals("")) {
            queryWrapper.like("right_text", "%" + rightText.trim() + "%");
        }
        queryWrapper.orderByAsc("right_code");
        IPage<RightEntity> ipage = rightService.getListBiz(page, queryWrapper);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("ipage", ipage);
        model.addAttribute("rightCode", rightCode);
        model.addAttribute("rightText", rightText);
        return "/right/list";
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map del(String rightCode) {
        Map map = new HashMap();
        int count = 0;
        try {
            count = rightService.deleteRightService(rightCode);
            if (count > 0) {
                map.put("delResult", "true");
            }
        } catch (Exception e) {
            map.put("delResult", "false");
            e.printStackTrace();
        }
        return map;
    }

    @GetMapping("/toadd")
    public String toAdd() {
        return "/right/add";
    }

    @PostMapping("/aliyunsave")
    public String aliyunsave(Model model, RightEntity rightEntity, MultipartFile uploadFile) {

        //service(完成1、增加业务表的数据 2.增加附件表的数据 3.将图片添加到阿里云oss上)
        //add();

        //1、增加业务表的数据
        int count = rightService.addRightService(rightEntity);
        //2.增加附件表的数据
        //fileService.addFileService(file);
        //3.将图片添加到阿里云oss上
        //String url = UploadAliYunUtil.uploadImage(uploadFile);
        if (count > 0) {
            return "redirect:/right/list";
        } else {
            model.addAttribute("msg", "添加失败");
            return "/right/add";
        }
    }

    @PostMapping("/save")
    public String save(MultipartFile uploadFile) {

        //String realPath = ("D:/uploadFile/");
        String realPath = System.getProperty("user.dir");
        realPath = realPath + "/ch12file/target/classes/static/upload/";
        System.out.println("realPath:" + realPath);
        //一：springboot获取当前项目路径的地址
        //输出:D:\code20220209\code\IdeaProjectsT330\S3\t330boot
        // System.out.println(System.getProperty("user.dir"));

        String format = sdf.format(new Date());
        //创建文件对象
        File folder = new File(realPath + format);
        String filePath = "";
        //判断上传的目录是否存在
        if (!folder.isDirectory()) {
            //如果上传的目录不存在就创建
            folder.mkdirs();
        }
        //得到上传文件真实名称
        String oldName = uploadFile.getOriginalFilename();
        //获取文件后缀
        String suffixName = oldName.substring(oldName.lastIndexOf("."));
        //生成文件名称UUID
        UUID uuid = UUID.randomUUID();
        //uuid + 文件前缀组成新的文件名
        String newName = uuid.toString() + suffixName;
        try {
            //把内存中图片写入磁盘
            uploadFile.transferTo(new File(folder, newName));
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败! ";
        }
        return "redirect:/right/list";
    }
}
